# -*- python -*-
# Copyright (C) 2003, zy_sunshine.
# Author:  zy_sunshine <zy.netsec@gmail.com>
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANT; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public LIcense for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.

import sys
import os
import stat
Import('*')

mi_src_dir = os.path.abspath('magic.installer')
mi_modules_dir=os.path.join(mi_src_dir, '_site-packages')

def ReadWriteFile(path_file, content_tuple):
	try:
		input = open('c:\Abafiles\Job-1.inp','r')
		out = open("exampleWrite.inp", "w")
		s = input.readlines()
		InputTuple = []
		for line in s:
			singleLine = line.rstrip()
			InputTuple.append(singleLine)
			out.write(singleLine)
			print len(InputTuple)
	except IOError, (errno, strerror):
		print "I/O error(%s): %s" % (errno, strerror)
	except ValueError:
	    print "Could not convert data to an integer."
	except:
		print "Unexpected error:", sys.exc_info()[0]
		raise
	input.close()
	out.close()

def write_file(path_file, content_tuple):
	try:
		out = open(path_file, "w")
		content_tuple = [line+'\n' for line in content_tuple]
		out.writelines(content_tuple)

	except IOError, (errno, strerror):
		print "I/O error(%s): %s" % (errno, strerror)
		raise
	except:
		print "Unexpected error:", sys.exc_info()[0]
		raise
	out.close()

def depPyModuleDebug(env, alias, dir, sofile, cfiles):
    sopath = os.path.join(mi_modules_dir, sofile)
    env.Command(sopath, ['setup.py'] + cfiles,
                ['cd %s; %s setup.py install --prefix=%s' % \
                 (dir, mi_config.pythonbin, mi_modules_dir)])
    env.Alias(alias, sopath)
    #env.Depends(alias, sopath)

#Export('mi_modules_dir')
Export('depPyModuleDebug')

# 1. Create pth file magicinstaller.pth
env.Alias('mi_debug', 'magicinstaller.pth')

def create_pth(target, source, env):
	content_tuple = [os.path.join(mi_modules_dir, 'lib/python2.6/site-packages'), mi_src_dir]
	write_file(os.path.join(mi_config.pylibdir, 'magiclinux.pth'), content_tuple)

env.Command('magicinstaller.pth', '', create_pth)

# 2. Create /etc/mbsave.py
if not os.path.isfile('/etc/mbsave.py'):
	write_file('/etc/mbsave.py', '')

# 3. Compile pyext lib and install them into src/magic.installer/_site-packages
SConscript('pyext/SConscript-debug')



# 4. m4 src/magic.installer/magic.values.xml AND
# 		src/magic.installer/mipublic.py
#
# 5. Dealing with src/magic.installer/image/banner.png
#
# 6. Dealing with src/magic.installer/gtk_themes
#
# 7. Dealing with src/magic.installer/po AND
# 				  src/magic.installer/operations/po
SConscript('magic.installer/SConscript-debug')


# Please modify src/magic.installer/_site-packages/mipublic.py at last.
# Chang the DATADIR variable to the absolute path of directory src/magic.installer
